<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
/**
 * This is the model class for table "engine".
 *
 * @property integer $id
 * @property string $title
 * @property integer $submodel_id
 * @property string $make
 * @property string $year
 * @property string $model
 * @property string $submodel
 * @property string $num_parts
 */
class Engine extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'engine';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['title', 'submodel_id', 'make', 'year', 'model', 'submodel', 'num_parts'], 'required'],
            [['submodel_id', 'num_parts'], 'integer'],
            [['title', 'make', 'model', 'submodel'], 'string', 'max' => 255],
            [['year'], 'string', 'max' => 4],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => 'Title',
            'submodel_id' => 'Submodel ID',
            'make' => 'Make',
            'year' => 'Year',
            'model' => 'Model',
            'submodel' => 'Submodel',
            'num_parts' => 'Num Parts',
        ];
    }

    /********************  自定义方法从这里开始  ***************************/

    /**
     * 该方法顾名思义
     * @param $params
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = self::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id
        ]);
        $query->andFilterWhere([
            'submodel_id' => $this->submodel_id
        ]);
        $query->andFilterWhere([
            'num_parts' => $this->num_parts
        ]);

        $query->andFilterWhere(['like', 'title', $this->title])
            ->andFilterWhere(['like', 'make', $this->make])
            ->andFilterWhere(['like', 'year', $this->year])
            ->andFilterWhere(['like', 'model', $this->model])
            ->andFilterWhere(['like', 'submodel', $this->submodel]);


        return $dataProvider;
    }

    /**
     * 根据id来找
     * @param $id
     * @return static
     * @throws NotFoundHttpException
     */
    public static function findModel($id)
    {
        if (($model = self::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

    /**
     * 根据 parts 寻找
     * @param $partsId
     */
    public static function findALlByPartsId( $partsId ){

        $sql = "SELECT 
  * 
FROM
  engine2part e2p 
  LEFT JOIN `engine` 
    ON e2p.`engine_id` = `engine`.`id` 
WHERE e2p.`part_id` = '" . $partsId . "';";

        $res = Yii::$app->db->createCommand( $sql )->queryAll();
        return $res;

    }

    public static function joinPartsAndEngine( $part_id, $engine_id ){

        $deleteSql = "DELETE FROM engine2part WHERE part_id='$part_id' AND engine_id='$engine_id'";
        $res = Yii::$app->db->createCommand( $deleteSql )->execute();

        $sql = "INSERT INTO engine2part SET part_id='$part_id', engine_id='$engine_id'";

        $res = Yii::$app->db->createCommand( $sql )->execute();
        return $res;

    }

    public static function unjoinPartsAndEngine( $part_id, $engine_id ){

        //todo : create the sql
        $deleteSql = "DELETE FROM engine2part WHERE part_id='$part_id' AND engine_id='$engine_id'";
        $res = Yii::$app->db->createCommand( $deleteSql )->execute();
        return $res;

    }
}
